import axios from 'axios';
import { Indicator, MessageBox } from 'mint-ui';

// const isDev = process.env.NODE_ENV === 'development';
const ajax = axios.create({
  // baseURL: isDev ? 'http://rap2api.taobao.org/app/mock/84997' : '你真实的地址',
});

ajax.interceptors.request.use((config) => {
  Indicator.open({
    text: '加载中...',
    spinnerType: 'fading-circle',
  });
  return config;
});
// ajax.interceptors.response.use((resp) => {
//   let ret = null;
//   if (resp.data.code === 200) {
//     ret = resp.data.data;
//   } else {
//     ret = resp.data.errMessage;
//   }
//   return ret;
// });

ajax.interceptors.response.use((resp) => {
  let ret = null;
  if (resp.status === 200) {
    ret = Promise.resolve(resp.data.data);
  } else {
    const errMessage = resp.data.msg || '未知错误, 请稍后重试！';
    MessageBox({
      title: '哎呀！出错了！',
      message: errMessage,
      confirmButtonText: '好的，晓得了！',
    });
  }
  Indicator.close();
  return ret;
});

export default ajax;

export const getHomeSwiper = () => {
  const now = new Date().getTime();
  return ajax.get(`/mz/v4/api/billboard/home?__t=${now}`);
};

export const getGoodsList = (config) => {
  const now = new Date().getTime();
  const {
    page,
    count,
  } = config;
  return ajax.get(`/mz/v4/api/film/now-playing?__t=${now}&page=${page}&count=${count}`);
};

export const getAllGoods = () => getGoodsList({ page: 1, count: 10000 });

export const getDetail = (id) => {
  const now = new Date().getTime();
  return ajax.get(`/mz//v4/api/film/${id}?__t=${now}`);
};
